﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PetaPoco;
namespace ZhongheCms.Models
{
    public partial class GswTblProInfo
    {
        [ResultColumn]
        public List<ProMasterType> ProTypes { get; set; }
        [ResultColumn]
        public List<GswTblContent> ProApp { get; set; }
        [ResultColumn]
        public List<GswTblProList> ProLists { get; set; }
        [ResultColumn]
        public GswTblProPic GswTblProPic{ get; set; }
        [ResultColumn]
        public List<GswTblProPic> GswTblProPiclist { get; set; }
        public static List<GswViewTblProInfo> GetProList(string title, int funcId=0, int cusid=0)
        {
            //FunctionId='" + funcId + "' and
            if (funcId == 0)
                funcId = 7;
            var strSql = "select * from GswViewTblProInfo where FunctionId="+funcId+" and CustomerId=" + cusid;
            if (!string.IsNullOrWhiteSpace(title))
            {
                strSql += " and  ProName like '%" + title + "%' ";
            }

            strSql += " order by ProNo";

            return GswViewTblProInfo.Fetch(strSql);
        }

        public static List<ProMasterType> GetProTypes( int cusid,string p="",string f="")
        {
            var fid = f;
            //if (!string.IsNullOrEmpty(p))
            //{
            //    var funs = GswTblFunction.SingleOrDefault("where FuncName='产品类别' and ActionParam='" + p + "'");
            //    if (funs != null)
            //    {
            //        fid = funs.Id;
            //    }
            //}
            var strSql = "select Id,MasterType from GswTblBaseInfo where MasterId=0 and RelateId=" + fid + " and CustomerId=" + cusid + " order by TypeNo";
            var mType = ZhongheRepo.GetInstance().Fetch<ProMasterType>(strSql);
            var subTypeExiste = GswTblBaseInfo.FirstOrDefault("where MasterId>0 and CustomerId=" + cusid + " and  FunctionId=" +37);
            if (subTypeExiste==null) return mType;
            foreach (var m in mType)
            {
                m.SubFlag = 1;
                strSql = "select Id,SubType from GswTblBaseInfo where MasterId="+m.Id+" Order By TypeNo";
                m.ProSubTypes = ZhongheRepo.GetInstance().Fetch<ProSubType>(strSql);
            }

            return mType;

        }
    }

    public class ProSubType
    {
        public int Id { get; set; }
        public string SubType { get; set; }
    }
    public class ProMasterType
    {
        public int Id { get; set; }
        public string MasterType { get; set; }
        public int SubFlag { get; set; }
        public List<ProSubType> ProSubTypes { get; set; }
        
    }
}